package com.healthtop.common;

import lombok.Data;

import java.util.List;

/**
 * Created by xiaoxiaobao on 2017/10/16.
 */
@Data
public class Page {
    //成员变量
    private String keyword = "";

    //当前页
    private int nowpage = 1;
    //总记录数
    private int countrecord;
    //总页数
    private int countpage;
    // 当前页记录开始的位置
    private int pageindex;
    // 每页显示的记录数
    public int pageSize = 20;
    // 开始的索引值
    private int startindex;
    // 结束的索引值
    private int endindex;
    //当前页信息
    private List allentities;
    //构造器
    public Page() {
    }
    public void setPageIndex(int countrecord, int nowpage) {
        // 计算当前页
        this.nowpage = nowpage;
        // 计算出当前页开始的位置
        this.pageindex = (nowpage - 1) * pageSize;
        // 计算总页数
        this.countrecord = countrecord;
        if (this.countrecord % this.pageSize == 0) {
            this.countpage = this.countrecord / this.pageSize;
        } else {
            this.countpage = this.countrecord / this.pageSize + 1;
        }
        //计算开始和结束的索引值
        //当当前页小于等于四时开始的索引值等于一,而结束的索引值分两种情况
        if (this.nowpage <= 5) {
            this.startindex = 1;
            if(countpage <=5) {
                this.endindex = countpage;
            }else{
                this.endindex = 5;
            }
        }
        // 当当前页大于四时开始的索引值和结束的索引值均分三种情况
        else if (this.nowpage > 5) {
//            if (this.endindex > this.countpage&& this.countpage < this.sumindex) {
//                this.startindex = 1;
//                this.endindex = this.countpage;
//            }
//            else if (this.countpage > this.sumindex) {
//                this.startindex = this.countpage - 5;
//                this.endindex = this.countpage;
//            }
//            else{
//                this.startindex = this.nowpage - 3;
//                this.endindex = this.nowpage + 2;
//            }
            this.endindex = this.nowpage+2;
            this.startindex = this.nowpage-2;
            if(countpage < endindex){
                endindex = countpage;
                startindex = endindex -5;
            }
        }
    }
}
